package com.jp.knowledge.my.activity;

import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;

import com.google.gson.JsonObject;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.broad.LocalBroadCast;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.logic.PersonLogic;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.my.model.OrganizaModel;
import com.jp.knowledge.my.resources.OrganizeJoinRes;
import com.jp.knowledge.util.ResultSubscriber;

import java.util.List;

public class OptionSchoolTimeActivity extends OptionActivity {
    @Override
    protected void init() {
        super.init();
        adapter.updateAdapter(getResources().getStringArray(R.array.join_school_time));
    }

    @Override
    public void itemSelect(int position) {
        joinToOrganization(position);
    }

    @Override
    public Intent putMessageToIntent(Intent intent, int position) {
        return intent;
    }


    private void joinToOrganization(int index){
        loading.show();
        Intent intent = getIntent();
        JsonObject param = new JsonObject();
        param.addProperty("token", HttpHelper.token);
        param.addProperty("id", intent.getStringExtra(OrganizeJoinRes.ID));
        int type = intent.getIntExtra(OrganizeJoinRes.REQUEST_CODE, 0);
        param.addProperty("type", type+1);
        param.addProperty("depaId", intent.getStringExtra(OrganizeJoinRes.DEPA_ID));
        String classId = intent.getStringExtra(OrganizeJoinRes.CLASS_ID);
        if(classId != null && classId.length() > 0)
            param.addProperty("classId", classId);
        String position = intent.getStringExtra(OrganizeJoinRes.RESPONSE_RESULT);
        if(position != null && position.length() > 0)
            param.addProperty("position", position);
        if(type == OrganizeJoinRes.SCHOOL_INDEX) {
            String joinSchoolTime = getResources().getStringArray(R.array.join_school_time)[index].replace("年", "");
            if (joinSchoolTime != null && joinSchoolTime.length() > 0)
                param.addProperty("startTime", joinSchoolTime);
        }
        HttpHelper.getInstance(this).commitJoinOrganize(param, type, new ResultSubscriber.OnResultListener(){
            @Override
            public void onStart(int requestType) {}
            @Override
            public void onCompleted(int requestType) {
                loading.cancel();
            }
            @Override
            public void onError(int requestType) {
                loading.cancel();
//                ToasUtil.toast(OptionSchoolTimeActivity.this, "抱歉，组织加入失败，请稍后重试");
            }
            @Override
            public void onNext(IModel data, int requestType) {
                if(data.getErrcode() != 0){
                    ToasUtil.toast(mContext, data.getMessage());
                    onError(requestType);
                    return;
                }
                List<OrganizaModel> organizaModels = data.getList(OrganizaModel.class);
                if(organizaModels != null && organizaModels.size() > 0) {
                    List<OrganizaModel> userOrganizes = application.getUserData().getCompanyList();
                    application.getUserData().setCompanyList(organizaModels);
                    for(OrganizaModel organizaModel : organizaModels){
                        if(!userOrganizes.contains(organizaModel)){
                            LocalBroadcastManager.getInstance(mContext)
                                    .sendBroadcast(new Intent(LocalBroadCast.COMPANY_JOIN)
                                        .putExtra("joinCompany", organizaModel)
                                        .putExtra("index", organizaModels.indexOf(organizaModel)));
                            break;
                        }
                    }
                }
//                OrganizaModel organizaModel = new OrganizaModel();
//                organizaModel.setCompanyType(getIntent().getIntExtra(OrganizeJoinRes.REQUEST_CODE, 0)+1);
//                organizaModel.setCompanyId(getIntent().getStringExtra(OrganizeJoinRes.ID));
//                organizaModel.setCompanyName(getIntent().getStringExtra(OrganizeJoinRes.COMPANY_NAME));
//                LocalBroadcastManager.getInstance(context)
//                        .sendBroadcast(new Intent(LocalBroadCast.COMPANY_JOIN)
//                                .putExtra("joinCompany", organizaModel));

//                updataUserData();
//                ToasUtil.toast(OptionSchoolTimeActivity.this, "组织加入成功");
                Intent intent = getIntent();
                intent.setClass(OptionSchoolTimeActivity.this, OrganizeActivity.class);
                startActivity(intent);
                OptionSchoolTimeActivity.this.finish();
            }
        });
    }

    private void updataUserData(){
        if(application.getUserData() == null
                || application.getUserData().getCompanyId() == null
                || application.getUserData().getCompanyId().trim().length() == 0){
            new PersonLogic(this).getUserData();
        }
    }
}
